package test.xml


class modify_xml {
    public static void main(String[] args) {

        def xmlDom = new XmlParser().parse(new File("test.xml"))
        def book1 = xmlDom.value.books.book[0]
        println 'count: ' + book1.@available
        book1.@available = (int) (Math.random() * 20)
        println 'new count: ' + book1.@available

        new File("test.xml").withPrintWriter { printWriter ->
            XmlNodePrinter nodePrinter = new XmlNodePrinter(printWriter);
            printWriter.println('<?xml version="1.0" encoding="utf-8"?>')
            nodePrinter.setPreserveWhitespace(true);
            nodePrinter.print(xmlDom);
            printWriter.flush()
        }
    }
}